package com.proj.hot.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.proj.hot.GlobalParams;
import com.proj.hot.R;
import com.proj.hot.utils.DebugUtil;
import com.proj.hot.utils.ImageLoaderConfigFactory;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 电影详情页
 */
public class MovieDetailActivity extends BaseActivity {

    private Button lookBtn;
    private Button payBtn;
    String contentid;
    JSONObject data;

    ImageView detail_poster;

    private TextView detail_name;
    private TextView detail_mz;
    private TextView detail_gs;
    private TextView detail_sy;
    private TextView detail_pf;
    private TextView detail_lx;
    private TextView detail_dy;
    private TextView detail_zy;
    private TextView detail_kd;
    private TextView detail_ispay;

    private TextView detail_more;

    private boolean thisContentIsPay = false;

    String mVideoUrl;
    String wxpic;
    String alpic;
    String price;
    int playpoint;


    String title;
    String introduction;
    String actor;
    String drector;
    String logo;

    private String paytype;

    int tryduration=0;




    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0) {
                setView();

            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movie_detail);

        lookBtn = (Button) this.findViewById(R.id.detail_btn_look);
        payBtn = (Button) this.findViewById(R.id.detail_btn_pay);
        lookBtn.setVisibility(View.INVISIBLE);
        payBtn.setVisibility(View.INVISIBLE);


        detail_poster = (ImageView) this.findViewById(R.id.detail_poster);
        detail_name = (TextView) this.findViewById(R.id.detail_name);
        detail_mz = (TextView) this.findViewById(R.id.detail_mz);
        detail_gs = (TextView) this.findViewById(R.id.detail_gs);
        detail_sy = (TextView) this.findViewById(R.id.detail_sy);
        detail_pf = (TextView) this.findViewById(R.id.detail_pf);
        detail_lx = (TextView) this.findViewById(R.id.detail_lx);
        detail_dy = (TextView) this.findViewById(R.id.detail_dy);
        detail_zy = (TextView) this.findViewById(R.id.detail_zy);
        detail_kd = (TextView) this.findViewById(R.id.detail_kd);
        detail_ispay = (TextView) this.findViewById(R.id.detail_ispay);
        detail_more = (TextView) this.findViewById(R.id.detail_more);
        detail_more.setFocusable(false);

        contentid = getIntent().getStringExtra("contentid");
        if (GlobalParams.screenHeight == 1080){
            detail_name.setTextSize(25);
            detail_ispay.setTextSize(20);

            detail_mz.setTextSize(28);
            detail_gs.setTextSize(22);
            detail_sy.setTextSize(22);
            detail_sy.setTextSize(22);
            detail_pf.setTextSize(22);
            detail_lx.setTextSize(22);
            detail_zy.setTextSize(22);
            detail_kd.setTextSize(22);
            detail_more.setTextSize(22);

        }


        getData();

        setOnListener();
    }

    private void setOnListener() {

        lookBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MovieDetailActivity.this, VODPlayActivity.class);
                intent.putExtra("playurl", mVideoUrl);
                if (lookBtn.getText().equals("播放")) {
                    intent.putExtra("freeDuratio", 0);
                    intent.putExtra("contentid", contentid);
                    intent.putExtra("playpoint", playpoint);
                } else {
                    intent.putExtra("freeDuratio", tryduration);
                    intent.putExtra("wxpic", wxpic);
                    intent.putExtra("alpic", alpic);
                    intent.putExtra("price", price);
                    intent.putExtra("contentid", contentid);

                }
                startActivity(intent);
            }
        });


        payBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MovieDetailActivity.this, PayOneActivity.class);
                intent.putExtra("wxpic", wxpic);
                intent.putExtra("alpic", alpic);
                intent.putExtra("price", price);
                intent.putExtra("contentid", contentid);


                startActivity(intent);
            }
        });
        lookBtn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
//                    lookBtn.setTextColor(getResources().getColor(R.color.baise));
//                    lookBtn.setBackgroundResource(R.color.lanse);
                } else {
//                    lookBtn.setTextColor(getResources().getColor(R.color.lanse));
//                    lookBtn.setBackgroundResource(R.color.baise);
                }
            }
        });
        payBtn.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
//                    payBtn.setTextColor(getResources().getColor(R.color.baise));
//                    payBtn.setBackgroundResource(R.color.lanse);
                } else {
//                    payBtn.setTextColor(getResources().getColor(R.color.lanse));
//                    payBtn.setBackgroundResource(R.color.baise);
                }
            }
        });
        detail_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MovieDetailActivity.this, MovieDetailMoreActivity.class);
                intent.putExtra("logo", logo);
                intent.putExtra("title", title);
                intent.putExtra("drector", drector);
                intent.putExtra("actor", actor);
                intent.putExtra("introduction", introduction);
                intent.putExtra("contentid", contentid);
                startActivity(intent);
            }
        });
        detail_more.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    detail_more.setTextColor(getResources().getColor(R.color.chengse));
                } else {
                    detail_more.setTextColor(getResources().getColor(R.color.qianhuise));
                }
            }
        });

        if (GlobalParams.user_paytype.equals("3")) {
            getPlayUrl();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        DebugUtil.i("MovieDetailActivity","执行了onResume（）");

        if (GlobalParams.user_paytype.equals("1")||GlobalParams.user_paytype.equals("3")) {
            DebugUtil.i("MovieDetailActivity","user_paytype="+GlobalParams.user_paytype);
            getPlayUrl();
        }


        if(GlobalParams.user_paytype.equals("2")){
            DebugUtil.i("MovieDetailActivity","user_paytype="+GlobalParams.user_paytype);
            this.finish();
        }


    }

    private void getData() {

        try {
            /**
             * ---------------body------------------
             */
            JSONObject body = new JSONObject();
            body.put("contentid", contentid);
            body.put("accountid", GlobalParams.user_id);
            body.put("sn", GlobalParams.SN);
            body.put("mac", GlobalParams.MAC_ADDRESS);


            RequestParams params = new RequestParams(GlobalParams.reqHost + "content/getContent");
            Log.i("getcontent","请求地址:"+GlobalParams.reqHost + "content/getPlayUrl");
            params.setAsJsonContent(true);
            params.setBodyContent(body.toString().trim());

            /**
             * ---------------send------------------
             */
            x.http().post(params, new org.xutils.common.Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    try {
                        DebugUtil.i("getcontent", "getContent=" + s);
                        JSONObject jData = new JSONObject(s);

                        JSONObject respbody = jData.getJSONObject("respbody");
                        data = respbody.getJSONObject("data");
                        DebugUtil.i("getcontent", "data=" + data);
                        handler.sendEmptyMessage(0);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFinished() {

                }

                @Override
                public void onCancelled(CancelledException e) {

                }

                @Override
                public void onError(Throwable throwable, boolean b) {
                    DebugUtil.toast(MovieDetailActivity.this, "获取详情失败，请退出重试！");
                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    void setView() {
        try {
            //ImageLoader.getInstance().displayImage(data.getString("logo"),detail_poster);
            ImageLoader.getInstance().displayImage(data.getString("logo"),
                    detail_poster, ImageLoaderConfigFactory.getOptionsWithNopic());
            detail_name.setText("<  "+data.getString("title"));
            detail_mz.setText(data.getString("title"));
            detail_gs.setText(data.getString("area"));
            detail_sy.setText(data.getString("releasetime"));
            detail_pf.setText(data.getString("compere") + "分");
            detail_lx.setText(data.getString("type"));
            detail_dy.setText(data.getString("director"));
            detail_zy.setText(data.getString("protagonist"));
            detail_kd.setText(data.getString("introduction"));
            String money = data.getString("money");
            paytype = data.getString("paytype");
            detail_ispay.setText(paytype);

            logo = data.getString("logo");
            title = data.getString("title");
            introduction = data.getString("introduction");
            drector = data.getString("director");
            actor = data.getString("protagonist");
            tryduration = data.getInt("tryduration");

            if(null == introduction||"".equals(introduction)){
                detail_more.setVisibility(View.GONE);
            }else{
                detail_more.setVisibility(View.VISIBLE);
            }

//            if("免费".equals(paytype)){
//                detail_ispay.setText("免费");
//            }else if("付费".equals(paytype)){
//                detail_ispay.setText("付费");
//            }

        } catch (JSONException e) {
            e.printStackTrace();
        }


    }


    private void getPlayUrl() {//单点鉴权


        try {


            /**
             * ---------------body------------------
             */

//            {"sn":"设备号","conentid":"购买影片Id","accountid":"用户id"}
            JSONObject body = new JSONObject();
            body.put("sn", GlobalParams.SN);
            body.put("contentid", contentid);
            body.put("accountid", GlobalParams.user_id);
            body.put("paytype", paytype);



            RequestParams params = new RequestParams(GlobalParams.reqHost + "content/getPlayUrl");
            Log.i("MovieDetailActivity","请求地址:"+GlobalParams.reqHost + "content/getPlayUrl");
            params.setAsJsonContent(true);
            params.setBodyContent(body.toString().trim());

            /**
             * ---------------send------------------
             */
            x.http().post(params, new org.xutils.common.Callback.CommonCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    try {
                        DebugUtil.i("MovieDetailActivity", "getPlayUrl=" + s);
                        JSONObject jData = new JSONObject(s);

                        JSONObject respbody = jData.getJSONObject("respbody");
                        String ispay = respbody.getJSONObject("data").getString("ispay");

                        if (ispay.equals("1")) {
                            lookBtn.setText("播放");
                            payBtn.setVisibility(View.INVISIBLE);
                            lookBtn.setVisibility(View.VISIBLE);
                            thisContentIsPay = true;

                        } else {

                            wxpic = respbody.getJSONObject("data").getString("wxpayurl");
                            alpic = respbody.getJSONObject("data").getString("zfbpayurl");
                            price = respbody.getJSONObject("data").getString("price");
                            lookBtn.setText("试看");
                            payBtn.setVisibility(View.VISIBLE);
                            lookBtn.setVisibility(View.VISIBLE);

                        }


                        if (GlobalParams.user_paytype.equals("3")) {
                            lookBtn.setText("播放");
                            payBtn.setVisibility(View.INVISIBLE);
                            lookBtn.setVisibility(View.VISIBLE);
                            thisContentIsPay = true;
                        }

                        mVideoUrl = respbody.getJSONObject("data").getString("playurl");
                        playpoint = respbody.getJSONObject("data").getInt("playpoint");
                        Log.i("VODPlayer", "拿回观看记录"+playpoint);

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }

                @Override
                public void onFinished() {

                }

                @Override
                public void onCancelled(CancelledException e) {

                }

                @Override
                public void onError(Throwable throwable, boolean b) {
                    DebugUtil.toast(MovieDetailActivity.this, "获取播放地址失败，请退出重试！");

                }
            });


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_DPAD_UP
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            detail_more.setFocusable(true);
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

}
